<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 商品分类管理
 */
namespace app\bestbao\controller;
use app\bestbao\model\BestbaoCategory;
use app\bestbao\model\BestbaoProduct;
use think\facade\Request;

class Category extends Common{


    public function initialize(){
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'产品分类','url'=>'javascript:;']]);
    }

    /**
     * 列表
     */
    public function index(){
        $view['lists'] = BestbaoCategory::where(['member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->paginate(20); 
        return view()->assign($view);
    }
    
    /**
     * 添加
     */
    public function add(){
        if(request()->isAjax()){
            $data = [
                'title'             => $this->request->param('title/s'),
                'note'              => $this->request->param('note/s'),
                'sort'              => $this->request->param('sort/d'),
                'picture'           => $this->request->param('picture/s'),
                'member_miniapp_id' => $this->member_miniapp_id,
            ];
            $validate = $this->validate($data,'Category.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result =  BestbaoCategory::edit($data);
            if($result){
                return enjson(200,'操作成功',['url'=>url('category/index')]);
            }else{
                return enjson(0);
            }
        }else{
            return view();
        }
    }

    //编辑
    public function edit(){
        if(request()->isAjax()){
            $data = [
                'id'      => $this->request->param('id/s'),
                'title'   => $this->request->param('title/s'),
                'note'    => $this->request->param('note/s'),
                'sort'    => $this->request->param('sort/d'),
                'picture' => $this->request->param('picture/s'),
            ];
            $validate = $this->validate($data,'Category.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result =  BestbaoCategory::edit($data);
            if($result){
                return enjson(200,'操作成功',['url'=>url('category/index')]);
            }else{
                return enjson(0);
            }
        }else{
            $view['info'] = BestbaoCategory::where(['id' => Request::param('id/d'),'member_miniapp_id' => $this->member_miniapp_id])->find();
            return view()->assign($view);
        }
    }

    /**
     * 排序
     */
    public function sort(){
        if(request()->isAjax()){
            $data = [
                'sort' => $this->request->param('sort/d'),
                'id'   => $this->request->param('id/d'),
            ];
            $validate = $this->validate($data,'Category.sort');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = BestbaoCategory::update(['sort'=>$data['sort']],['id' => $data['id']]);
            if($result){
                return enjson(200);
            }else{
                return enjson(0);
            }
        }
    }

    //删除
    public function delete(int $id){
        $goods = BestbaoProduct::where(['category_id' => $id,'member_miniapp_id' => $this->member_miniapp_id])->find();
        if($goods){
            return enjson(403,'删除失败,栏目中还包含产品');
        }
        $result = BestbaoCategory::destroy($id);
        if($result){
            return enjson(200);
        }else{
            return enjson(403,'删除失败');
        } 
    }
}